package com.tyqx.hashchainbiz.controller.donate.product;

import com.tyqx.hashchainbean.module.DonateProduct;
import com.tyqx.hashchainbean.vo.QueryParamVo;
import com.tyqx.hashchainbean.vo.QueryTimeParamVo;
import com.tyqx.hashchainbiz.service.DonateProductService;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(description = "物资捐赠")
@RestController
@RequestMapping("/donateProduct")
@CrossOrigin
public class DonateProductController {

    @Resource
    private DonateProductService donateProductService;

    @RequestMapping(value="/saveDonateProduct", method = RequestMethod.POST)
    @ResponseBody
    public String saveDonateProduct(@RequestBody DonateProduct donateProduct) throws Exception {
        String msg="";

        if(donateProductService.donateProduct(donateProduct)>0){
            msg="物资捐赠成功";
        }else{
            msg="物资捐赠失败";
        }
        return msg;
    }

    @RequestMapping(value="/getDonateProductById", method = RequestMethod.POST,
            produces = "application/json")
    @ResponseBody
    public List<DonateProduct> getDonateProductById(@RequestBody QueryParamVo queryParamVo) throws Exception {
        List<DonateProduct> donateProducts=donateProductService.getDonateProductById(queryParamVo.getDonateId(),queryParamVo.getUserName());
//        for (int i=0;i<donateProducts.size();i++){
//            String attachmentHashValue=donateProducts.get(i).getAttachment();
//            Criteria criteria=Criteria.where("hashValue").is(attachmentHashValue);
//            Query query=Query.query(criteria);
//            List<ImageVO> imageVO=mongoTemplate.find(query,ImageVO.class,"donateProduct");
//            donateProducts.get(i).setAttachment(imageVO.get(0).getAttachment());
//        }
        return donateProducts;
    }

    @RequestMapping(value="/getDonateProductByTime", method = RequestMethod.POST,
            produces = "application/json")
    @ResponseBody
    public List<DonateProduct> getDonateCashByTime(@RequestBody QueryTimeParamVo queryTimeParamVo) throws Exception {

        List<DonateProduct> donateProducts=donateProductService.getDonateProductByTime(queryTimeParamVo.getStartTime(),
                queryTimeParamVo.getEndTime(),queryTimeParamVo.getUserName());
        /*for (int i=0;i<donateProducts.size();i++){
            String attachmentHashValue=donateProducts.get(i).getAttachment();
            Criteria criteria=Criteria.where("hashValue").is(attachmentHashValue);
            Query query=Query.query(criteria);
            List<ImageVO> imageVO=mongoTemplate.find(query,ImageVO.class,"donateProduct");
            donateProducts.get(i).setAttachment(imageVO.get(0).getAttachment());
        }*/
        return donateProducts;
    }
}
